﻿using CookComputing.XmlRpc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;

namespace YuuzleComic.Common
{
    public struct GoogleReturn
    {
        public bool flerror;
        public string message;
    }

    public class PingXML : XmlRpcClientProtocol
    {
        [XmlRpcMethod("weblogUpdates.extendedPing")]
        public int BaiduPing(string blogName, string blogURL, string newPostURL, string blogRSS)
        {
            return (int)this.Invoke("BaiduPing",
                new object[] { blogName, blogURL, newPostURL, blogRSS });
        }

        [XmlRpcMethod("weblogUpdates.extendedPing")]
        public GoogleReturn GooglePing(string blogName, string blogURL, string newPostURL, string blogRSS)
        {
            return (GoogleReturn)this.Invoke("GooglePing",
                new object[] { blogName, blogURL, newPostURL, blogRSS });
        }
    }


    public class Blog
    {
        public string BlogName { get; set; }
        public string BlogURL { get; set; }
        public string SiteMap { get; set; }
        public string SiteRSS { get; set; }
        public List<string> GetPostURL()
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            List<string> url = new List<string>();

            request = (HttpWebRequest)WebRequest.Create(this.SiteMap);
            response = (HttpWebResponse)request.GetResponse();
            reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string html = reader.ReadToEnd();

            Regex reg = new Regex("<loc>(.*?)</loc>");
            MatchCollection mc = reg.Matches(html);
            foreach (Match item in mc)
            {
                url.Add(item.Groups[1].Value);
            }

            return url;
        }
    }
}
